package Boot.test.Filter;

import org.springframework.util.CollectionUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//@WebFilter(filterName = "testFilter", urlPatterns = "/*",
//        initParams = @WebInitParam(name = "noFilterUrl", value = "/test"))
//  注解属性详解
// filterName 过滤器名称 urlPatterns 值是一个数组 指定拦截的路径  displayName 过滤器名称
// servletNames 值是一个数组，指定对哪些servlet进行过滤 initParams进行一些基本配置
@WebFilter
public class AutoFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        在请求前拦截
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
//        System.out.println(111);

//         servletResponse1.setStatus(405);
//         return;  //不匹配返回

        System.out.println(11);
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");//指定授权访问的域，*允许所有url访问
        response.setHeader("Access-Control-Allow-Methods", "*");//授权请求的方法
        response.setHeader("Access-Control-Max-Age", "3600");//60 min
        response.setHeader("Access-Control-Allow-Headers", "*");//响应头部
        response.setHeader("Access-Control-Allow-Credentials", "true");//允许客户端携带验证信息
        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {

    }
}
